package com.study.talk.module.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.common.resullt.FwResult;
import com.study.talk.module.entity.MomentComment;
import com.study.talk.module.entity.MomentLike;
import com.study.talk.module.entity.vo.MomentCommentVo;
import com.study.talk.module.entity.vo.MomentLikeVo;
import com.study.talk.module.service.MomentCommentService;
import com.study.talk.module.service.MomentLikeService;
import com.study.talk.module.service.MomentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Null;
import java.util.ArrayList;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author thor
 * @since 2024-3-13
 */
@RestController
@RequestMapping("/moment_comment")
public class MomentCommentController {
    @Resource
    private MomentService momentService;

    @Resource
    private MomentLikeService momentLikeService;

    @Resource
    private MomentCommentService momentCommentService;

    @GetMapping("/get/{id}")
    @ApiOperation("获取评论记录")
    public FwResult<MomentComment> getMomentLike(@PathVariable("id") Integer id) {
        MomentComment momentComment = momentCommentService.getById(id);
        if (momentComment != null) return FwResult.ok(momentComment);
        else return FwResult.failed();
    }

    @PostMapping("/add")
    @ApiOperation("添加评论记录")
    public FwResult<ArrayList<MomentComment>> addMomentLike(@RequestBody MomentCommentVo momentCommentVo) {
        if (momentCommentService.save(new MomentComment(momentCommentVo))) {
            return FwResult.ok(momentCommentService.getListByMomentId(momentCommentVo.getMomentId()));
        }
        return FwResult.failed();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除评论记录")
    public FwResult<ArrayList<MomentComment>> deleteMomentLike(@RequestBody MomentComment momentComment) {
        if (momentCommentService.removeById(momentComment)) {
            return FwResult.ok(momentCommentService.getListByMomentId(momentComment.getMomentId()));
        }
        return FwResult.failed();
    }

    @PutMapping("/update")
    @ApiOperation("更新评论记录")
    public FwResult<ArrayList<MomentComment>> updateMomentLike(@RequestBody MomentComment momentComment) {
        if (momentCommentService.updateById(momentComment)) {
            return FwResult.ok(momentCommentService.getListByMomentId(momentComment.getMomentId()));
        }
        return FwResult.failed();
    }

    @GetMapping("/get/list/{momentId}")
    @ApiOperation("根据动态id查找评论列表")
    public FwResult<ArrayList<MomentComment>> getListByMomentId(@PathVariable("momentId") Integer momentId) {
        return FwResult.ok(momentCommentService.getListByMomentId(momentId));
    }


}
